﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace MangaUtils
{
	/// <summary>
	/// Class which represents single manga project.
	/// </summary>
	public class Manga
	{
		/// <summary>
		/// Manga title.
		/// </summary>
		public string Title;
		/// <summary>
		/// Creator studio or mangaka.
		/// </summary>
		public string AuthorStudio;
		/// <summary>
		/// Absolute path to cover image. Necessary field!
		/// </summary>
		public string CoverPath;
		/// <summary>
		/// Manga project folder with its subfolders.
		/// </summary>
		public Folder ProjectFolder;

		/// <summary>
		/// DO NOT USE!!!
		/// Created only for needs of XmlSerializer.
		/// </summary>
		public Manga()
		{
			Title = string.Empty;
			AuthorStudio = string.Empty;
			CoverPath = string.Empty;
			ProjectFolder = new Folder();
		}

		/// <summary>
		/// The one and only constructor to be used.
		/// </summary>
		/// <param name="path">Absolute path to manga folder.</param>
		/// <param name="title">Manga title.</param>
		/// <param name="author">Author (mangaka's) name or studio title.</param>
		/// <param name="coverPath">Absilute path to cover image.</param>
		public Manga(string path, string title, string author, string coverPath)
		{
			Title = title;
			AuthorStudio = author;
			ProjectFolder = new Folder(path);
			CoverPath = coverPath;
		}

		/// <summary>
		/// Forms list of images for Book Control.
		/// </summary>
		/// <param name="volumeNumber">Volume number in range [0..∞).</param>
		/// <returns>List of ready-to-be-used Images for Mitsu's BookControl.</returns>
		public List<Image> GetVolumeFiles(int volumeNumber)
		{
			var images = new List<Image>();

			int chapterCount = ProjectFolder.Subfolders[volumeNumber].Subfolders.Count;
			if (chapterCount != 0)
			{
				for (int chapterNumber = 0; chapterNumber < chapterCount; chapterNumber++)
				{
					string chapPath = ProjectFolder.Subfolders[volumeNumber].Subfolders[chapterNumber].Path + @"\";
					images.AddRange(FillImageList(chapPath));
				}
			}
			else
			{
				string volPath = ProjectFolder.Subfolders[volumeNumber].Path;
				images.AddRange(FillImageList(volPath));
			}

			return images;
		}

		/// <summary>
		/// Forms list of images for Book Control. Overloaded version for any folder with pictures.
		/// </summary>
		/// <returns>List of ready-to-be-used Images for Mitsu's BookControl.</returns>
		public List<Image> GetVolumeFiles()
		{
			var images = new List<Image>();

			int chapterCount = ProjectFolder.Subfolders.Count;
			if (chapterCount != 0)
			{
				for (int chapterNumber = 0; chapterNumber < chapterCount; chapterNumber++)
				{
					string chapPath = ProjectFolder.Subfolders[chapterNumber].Path + @"\";
					images.AddRange(FillImageList(chapPath));
				}
			}
			else
			{
				string volPath = ProjectFolder.Path;
				images.AddRange(FillImageList(volPath));
			}

			return images;
		}

		#region Utility methods
		/// <summary>
		/// Fills image list with Images, ready to be used in BookControl.
		/// Crops image into two if necessary.
		/// </summary>
		/// <param name="path">Path to chapter or volume.</param>
		/// <returns>List of cropped Images.</returns>
		private List<Image> FillImageList(string path)
		{
			var images = new List<Image>();
			var filesNames = new List<string>(Directory.GetFiles(path, "*.jpg"));
			filesNames.AddRange(new List<string>(Directory.GetFiles(path, "*.png")));
			filesNames.AddRange(new List<string>(Directory.GetFiles(path, "*.gif")));
			foreach (var file in filesNames)
			{
				var i = new Image();
				i.Source = new BitmapImage(new Uri(file));

				if (images.Count == 1) //temporary workout for image cover
					images.Add(new Image());

				if (i.Source.Width > i.Source.Height)
				{
					//dirty hack ^_^
					if (images.Count == 2)
						images.RemoveAt(1);
					else
						if (images.Count != 0)
						{
							if (images.Count % 2 == 0)
								images.Add(new Image());
						}
						else images.Add(new Image { Source = new BitmapImage(new Uri(CoverPath)) });

					var i1 = new Image();
					var i2 = new Image();

					var b = i.Source as BitmapSource;

					int startX = b.PixelWidth / 2;
					int height = b.PixelHeight;

					var bitmap1 = new CroppedBitmap(b, new Int32Rect(0, 0, startX, height));
					var bitmap2 = new CroppedBitmap(b, new Int32Rect(startX, 0, startX, height));

					i1.Source = bitmap1;
					i2.Source = bitmap2;

					images.Add(i1);
					images.Add(i2);
				}
				else
					images.Add(i);
			}
			if (images.Count % 2 == 1) //some offset -- to show end of chapter and start new chapter always in right page
				images.Add(new Image());

			return images;
		}
		#endregion
	}
}
